package com.bluefish.bltuan.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bluefish.bltuan.dao.CategoryDao;
import com.bluefish.bltuan.dao.ShopDao;
import com.bluefish.bltuan.module.Category;
import com.bluefish.bltuan.module.shop.TuanGou;
import com.bluefish.bltuan.service.ShopService;
import com.bluefish.bltuan.tools.DateUtils;
import com.bluefish.bltuan.util.PageIndex;

@Service
@Transactional
public class ShopServiceImpl implements ShopService{

	private ShopDao shopDao;
	
	private CategoryDao categoryDao;
	
	public boolean publish(TuanGou t) {
		if(t != null && getTuanGouAtDateAndCity(t.getPublishTime(),t.getCity()) == null){
			t.setPublished(true);
			this.shopDao.update(t);
			return true;
		}else{
			return false;
		}
	}
	
	private TuanGou getTuanGouAtDateAndCity(Date publishedDate, Category city) {
		String publishedTime = DateUtils.getDateString(publishedDate);
		List<TuanGou> tuans = this.shopDao.listAtDateAndCity(publishedTime, city); 
		return tuans != null && tuans.size() > 0 ? tuans.get(0) : null;
	}

	public void unpublish(TuanGou t) {
		if(t != null){
			t.setPublished(false);
			this.shopDao.update(t);
		}
	}
	
	public TuanGou getTuangouToday() {
		List<TuanGou> tuanGous = this.shopDao.list(new Date(),false);
		return tuanGous != null && tuanGous.size() > 0 ? tuanGous.get(0) : null;
	}
	
	public List<TuanGou> listOldTuangou() {
		List<TuanGou> tuanGous = this.shopDao.listBefore(new Date(),false);
		return tuanGous;
	}
	
	public List<TuanGou> listOldTuangou(PageIndex pageIndex) {
		List<TuanGou> tuanGous = this.shopDao.listBefore(new Date(), pageIndex, false);
		return tuanGous;
	}
	
	public List<TuanGou> listOldTuangou(int size) {
		List<TuanGou> tuanGous = this.shopDao.listBefore(new Date() ,size, false);
		return tuanGous;
	}
	
	public List<TuanGou> list(PageIndex pageIndex) {
		return this.shopDao.list(pageIndex);
	}
	
	public void delete(TuanGou t) {
		this.shopDao.delete(t);
	}

	public TuanGou findById(Integer id) {
		return this.shopDao.findById(id);
	}

	public void save(TuanGou t) {
		this.shopDao.save(t);
	}
	
	public void saveOrUpdate(TuanGou t) {
		this.shopDao.saveOrUpdate(t);
	}

	public void update(TuanGou t) {
		t.setCreateTime(new Date());
		this.shopDao.update(t);
	}

	public List<TuanGou> listOldTuangou(PageIndex pageIndex, Category city) {
		return this.shopDao.listBeforeByCity(new Date(), pageIndex, city, false);
	}

	public void setShopDao(ShopDao shopDao) {
		this.shopDao = shopDao;
	}
	
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public List<TuanGou> listFutureTuangou(PageIndex pageIndex, Category city) {
		return this.shopDao.listAfterByCity(new Date(), pageIndex, city, false);
	}
	
	public TuanGou getTuangouToday(Integer cityId) {
		Category city = this.categoryDao.findById(cityId);
		return getTuanGouAtDateAndCity(new Date(), city);
	}

	public TuanGou getTuangouToday(Category city) {
		return getTuanGouAtDateAndCity(new Date(), city);
	}
	
	public TuanGou getPublishedTuangouToday(Category city) {
		return getTuanGouAtDateAndCity(new Date(), city);
	}

	public List<TuanGou> listPublishedOldTuans(PageIndex pageIndex,
			Category city) {
		return this.shopDao.listBeforeByCity(new Date(),
				pageIndex, city, true);
	}
	
	public List<TuanGou> listPublishedPastTuans(PageIndex pageIndex,
			Category city) {
		return this.shopDao.listNotAfterByCity(pageIndex, new Date(), city, true);
	}
	
	public TuanGou getPublishedTuanByNick(String nick) {
		List<TuanGou> tuans = this.shopDao.listByNick(nick, true);
		return tuans != null && tuans.size() > 0 ? tuans.get(0) : null;
	}
}
